package com.zhou.system.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zhou.common.lib.exception.BizException;
import com.zhou.common.lib.util.BeanUtil;
import com.zhou.system.management.constant.UserContext;
import com.zhou.system.management.entity.SysDept;
import com.zhou.system.management.entity.SysDeptUser;
import com.zhou.system.management.mapper.SysDeptMapper;
import com.zhou.system.management.model.ao.SysDeptAddAo;
import com.zhou.system.management.model.ao.SysDeptEnableAo;
import com.zhou.system.management.model.ao.SysDeptUpdateAo;
import com.zhou.system.management.model.vo.RouteVo;
import com.zhou.system.management.model.vo.UserRouteVo;
import com.zhou.system.management.security.model.MyUserDetails;
import com.zhou.system.management.service.ISysDeptService;
import com.zhou.system.management.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.zhou.system.management.enums.ManageErrorCodeEnum.USER_HAVE_NOT_SET_DEPT;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author zxf
 * @since 2023-05-18
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Autowired
    private SysDeptUserServiceImpl deptUserService;

    @Autowired
    private SysMenuServiceImpl menuService;

    @Override
    public List<SysDept> queryUserDepts(Long userId) {
        if (UserUtil.isAdmin(userId)) {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(-1L);
            return Lists.newArrayList(sysDept);
        }
        return baseMapper.queryUserDepts(userId);
    }

    @Override
    public void setUserCurrDept(Long deptId) {
        MyUserDetails myUserDetails = UserContext.currentUser();
        QueryWrapper<SysDeptUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct dept_id").lambda()
                .eq(SysDeptUser::getUserId, myUserDetails.getUserId())
                .eq(SysDeptUser::getDeptId, deptId);
        int count = deptUserService.count(queryWrapper);
        if (count == 0) {
            throw new BizException(USER_HAVE_NOT_SET_DEPT);
        }
        SysDept dept = getById(deptId);
        myUserDetails.setCurrOrgId(dept.getDeptId());
        myUserDetails.setCurrOrgName(dept.getDeptName());

    }

    @Override
    public UserRouteVo queryUserRoute() {
        UserRouteVo userRouteVo = new UserRouteVo();
        List<RouteVo> routeVos = menuService.queryMenuRoute();
        userRouteVo.setMenuInfo(routeVos);
        RouteVo homeInfo = new RouteVo();
        homeInfo.setTitle("首页");
        homeInfo.setHref("page/welcome-1.html?t=1");
        RouteVo logoInfo = new RouteVo();
        logoInfo.setTitle("LAYUI MINI");
        logoInfo.setImage("images/logo.png");
        userRouteVo.setHomeInfo(homeInfo);
        userRouteVo.setLogoInfo(logoInfo);
        return userRouteVo;
    }

    @Override
    public void add(SysDeptAddAo addVo) {
        SysDept sysDept = BeanUtil.copyObject(addVo, SysDept.class);
        save(sysDept);
    }

    @Override
    public void update(SysDeptUpdateAo updateVo) {
        SysDept sysDept = BeanUtil.copyObject(updateVo, SysDept.class);
        updateById(sysDept);
    }

    @Override
    public void enableDept(SysDeptEnableAo ao) {
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(ao.getDeptId());
        sysDept.setStatus(ao.getStatus());
        updateById(sysDept);
    }
}
